/*
 * monitor.h
 *
 *  Created on: 2011-5-30
 *      Author: chenl
 */

#ifndef MONITOR_H_
#define MONITOR_H_

#ifndef WIN32
#include <ncurses.h>
#endif

#include "common/common_type.h"
#include "common/common_err.h"

#define MAX_ROW			90
#define MAX_COLUMN		180

#define PCB_INFO_PID				"pid"
#define PCB_INFO_STATE				"state"
#define PCB_INFO_NEED_RESCHED		"need_resched"
#define PCB_INFO_POLICY				"policy"

#define PCB_INFO_COUNTER			"counter"
#define PCB_INFO_NICE				"nice"


#define PCB_INFO_PRIO				"prio"
#define PCB_INFO_STATIC_PRIO		"static_prio"
#define PCB_INFO_TIME_SLICE			"time_slice"
#define PCB_INFO_SLEEP_TIME			"sleep_time"
#define PCB_INFO_SLEEP_AVG			"sleep_avg"
#define PCB_INFO_PRIO_ARRAY			"prio_array"
#define PCB_INFO_TIME_STAMP			"time_stamp"
#define PCB_INFO_LAST_RAN			"last_ran"


#define PCB_INFO_FORMAT_O_N			PCB_INFO_PID"\t"\
									PCB_INFO_STATE"\t"\
									PCB_INFO_NEED_RESCHED"\t"\
									PCB_INFO_POLICY"\t"\
									PCB_INFO_COUNTER"\t"\
									PCB_INFO_NICE

#define PCB_INFO_FORMAT_O_1			PCB_INFO_PID"\t"\
									PCB_INFO_STATE"\t"\
									PCB_INFO_NEED_RESCHED"\t"\
									PCB_INFO_POLICY"\t"\
									PCB_INFO_PRIO"\t"\
									PCB_INFO_STATIC_PRIO"\t"\
									PCB_INFO_TIME_SLICE"\t"\
									PCB_INFO_SLEEP_TIME"\t"\
									PCB_INFO_SLEEP_AVG"\t"\
									PCB_INFO_PRIO_ARRAY


enum
{
	enmOutUpBound_Y 	= 0x80090001,
	enmOutLowBound_Y 	= 0x80090002,
	enmOutLeftBound_X	= 0x80090003,
	enmOutRightBound_X 	= 0x80090004,
};

enum
{
	enmMaxMointorTitleLen = MAX_COLUMN,
};

class CMonitor
{
public:
	CMonitor(int height, int width, int startY, int startX, const char* szTitle);
	~CMonitor();

public:
	int32_t Initialize();
	int32_t Uninitialize();

	char* GetWinTitle()
	{
		return szTitle_;
	}

	int GetHeight() const
	{
		return height_;
	}

	int GetWidth() const
	{
		return width_;
	}

	int GetStartX() const
	{
		return startX_;
	}

	int GetStartY() const
	{
		return startY_;
	}

	int GetWinCurrX() const
	{
		return currX_;
	}

	int GetWinCurrY() const
	{
		return currY_;
	}

	int32_t Update(char arrszContent[][MAX_COLUMN], unsigned lines, unsigned maxContentLen, int startLineNum, int highlight);

protected:
	int32_t CheckArea(int startY, int startX, int lines, int len)
	{
		if (startY < 0)
			return enmOutUpBound_Y;
		if (lines + startY > winContentHeight_)
			return enmOutLowBound_Y;
		if (startX < 0)
			return enmOutLeftBound_X;
		if (startX + len > winContentWidth_)
			return enmOutRightBound_X;
		return S_OK;
	}


private:
	int 	height_;
	int 	width_;
	int 	startY_;
	int 	startX_;

	int		winContentHeight_;
	int		winContentWidth_;
	int		winContentStartY_;
	int		winContentStartX_;
	int		currY_;
	int		currX_;

	char 	szTitle_[enmMaxMointorTitleLen];
	WINDOW*	pWin_;
	WINDOW* pWinContent_;
};

#endif /* MONITOR_H_ */
